package com.example.firstjavamqtt.factory;

import com.example.firstjavamqtt.Constants.TopicConstants;
import com.example.firstjavamqtt.factory.strategy.callback.*;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

//回调消息策略工厂模式
@Component
public class HandleMsgFactory {

    private static final Map<String, HandleMessageStrategy> strategies = new HashMap<>();

    static {
        strategies.put(TopicConstants.REGISTER,new HandleRegisterMsgStrategy());
        strategies.put(TopicConstants.HEART,new HandleHeartMsgStrategy());
        strategies.put(TopicConstants.TEMPERATURE_HUMIDITY,new HandleTempAndHumidityMsgStrategy());
        strategies.put(TopicConstants.INFRARED,new HandleInfraredMsgStrategy());
    }

    //处理方法
    public static void handle(String topic,String msg) {
        HandleMessageStrategy handleMessageStrategy = strategies.get(topic);
        if(handleMessageStrategy==null){
            System.out.println("暂无该主题对应的策略处理方法");
            return;
        }
        handleMessageStrategy.handle(msg);
    }
}
